package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyProductVO;
import com.huihuiyun.huihuiyun.server.domain.HhyProduct;
import com.huihuiyun.huihuiyun.server.domain.HhyProductCategory;
import com.huihuiyun.huihuiyun.server.service.HhyProductCategoryService;
import com.huihuiyun.huihuiyun.server.service.HhyProductService;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Vector;

/**
 * 商城， 商品， 收藏， 购物车
 */
@Component
@Path(Const.context)
@Slf4j
public class MemberClientMallController {
    @Context
    private HttpServletResponse httpServletResponse;

    @Autowired
    private HhyProductCategoryService hhyProductCategoryService;

    @Autowired
    private HhyProductService hhyProductService;

    /*******************
     * unite mall
     ******************/
    //fixme using resutVo
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findUniteMallProductCategoryList")
    public List findUniteMallProductCategoryList() {
        try {
            List list = hhyProductCategoryService.findUniteMallProductCategoryList();
            list = MemberControllerHelper.convertUniteMallProductCategory2ViewObject(list);
            return list;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }

    /*******************
     * point mall
     ******************/
    //fixme using resutVo
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findPointsMallProductCategoryList")
    public List findPointsMallProductCategoryList() {
        try {
            List<HhyProductCategory> list = hhyProductCategoryService.findPointsMallProductCategoryList();
            list = MemberControllerHelper.convertUniteMallProductCategory2ViewObject(list);
            return list;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }

    /*******************
     * product
     ******************/
    //fixme using resutVo
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findProductListByProductCategoryIdAndAreaCode")
    public List<HhyProduct> findProductListByProductCategoryIdAndAreaCode(@QueryParam("productCategoryId") String productCategoryId, @QueryParam("areaCode") String areaCode) {
        try {
            int status = HhyProduct.statusUp;
            int reviewStatus = HhyProduct.reviewStatus1Success;
            List<HhyProduct> list = hhyProductService.findAllByProductCategoryIdAndAreaCodeAndStatusAndReviewStatus(productCategoryId, areaCode, status, reviewStatus);
            if (list != null) {
                return list;
            } else {
                return new Vector();
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }

    //fixme using resutVo
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findProductListByProductCategoryId")
    public List<HhyProduct> findProductListByProductCategoryId(@QueryParam("productCategoryId") String productCategoryId) {
        try {
            int status = HhyProduct.statusUp;
            int reviewStatus = HhyProduct.reviewStatus1Success;
            List<HhyProduct> list = hhyProductService.findAllByCategoryIdAndStatusAndReviewStatus(productCategoryId, status, reviewStatus);
            if (list != null) {
                return list;
            } else {
                return new Vector();
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }

    //fixme using resutVo
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findProductById")
    public HhyProductVO findProductById(@QueryParam("id") String id) {
        try {
            HhyProduct uniteMallProduct = hhyProductService.findOne(id);
            HhyProductVO hhyProductVO = new HhyProductVO();
            BeanUtils.copyProperties(uniteMallProduct, hhyProductVO);
            return hhyProductVO;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }

}